import os
import shutil
import re
# List of folder and corresponding simulation parameters
folder_list = [('q_r2_1.00_q_m2_0.01',1.00,0.01),
               ('q_r2_1.00_q_m2_0.1',1.00,0.1),
               ('q_r2_1.00_q_m2_1.0',1.00,1.0),
               ('q_r2_1.00_q_m2_10',1.00,10),
               ('q_r2_1.00_q_m2_100',1.00,100)]
# Constants that are not changed in this set of simulations
r2_set = 170
m2_set = 0.8
time = 500
dt = 1.0
print "making the following folder(s) "
for i in range(len(folder_list)):
    folder=folder_list[i][0]
    q_r2=folder_list[i][1]
    q_m2=folder_list[i][2]
    print i, folder
    if os.path.exists(folder):
        pass
    else:
        os.mkdir(folder)
#    shutil.copy('./simulation.template',folder+'/simulation.sh')
    p_folder = re.compile('VAL_CUR_FOLDER')
    p_q_r2 = re.compile('VAL_Q_R2')
    p_q_m2 = re.compile('VAL_Q_M2')
    p_repeat = re.compile('VAL_REPEAT')
    if 'nt'==os.name:
        fi = open('./simulation.template.sh')
        fo = open(folder+'/simulation.sh','w')
    else:
        fi = open('./simulation.template.bat')
        fo = open(folder+'/simulation.bat','w')
    for line in fi:
        line = p_folder.sub(folder,line)
        line = p_q_r2.sub(str(q_r2),line)
        line = p_q_m2.sub(str(q_m2),line)
        line = p_repeat.sub(str(1),line)
        fo.write(line)
    fi.close()
    fo.close()
    # Submit jobs using qsub
    if 'posix'==os.name:
        os.system('qsub -v LD_LIBRARY_PATH -l h_rt=12:00:00 '+folder+'/simulation.sh')
#        command='qsub -v LD_LIBRARY_PATH -l h_rt=12:00:00 '+folder+'/simulation.sh'
 #       os.system('echo '+command)
 
    
        
    
    

    

